package org.swfer.UIC 
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * 滚动条
	 * 必须部件 up,down,bar,dragRect
	 * 回调事件 change
	 * @author swfer
	 */
	public class ScrollBar extends Component 
	{
		public static const CHANGE:String = 'change';
		
		protected var up:InteractiveObject;
		protected var down:InteractiveObject;
		protected var bar:Sprite;
		protected var dragRect:Sprite;
		protected var _interval:int=2;
		protected var _value:Number;
		
		public function ScrollBar(view:MovieClip) 
		{
			super(view);
		}
		
		///////////////////////////////////
		// methods
		///////////////////////////////////
		
		override public function dispose():void
		{
			if (disposed) return;
			super.dispose();
			
			up = null;
			down = null;
			bar = null;
			dragRect = null;
		}
		
		/**
		 * 重置滚动条状态
		 * @param	totalHeight  目标大高度
		 * @param	pageHeight   目标单页高度
		 * @param	itemNum      目标单页个数
		 */
		public function reset(totalHeight:int,pageHeight:int):void
		{
			view.stopDrag();
			if (totalHeight <= pageHeight)
			{
				view.visible = false;
			}
			
			bar.y = dragRect.y;
		}
		
		override protected function init():void
		{
			up = view['up'] as InteractiveObject;
			down = view['down'] as InteractiveObject;
			bar = view['bar'] as Sprite;
			dragRect = view['dragRect'] as Sprite;
			
			//down.buttonMode = true;
			bar.buttonMode = true;
		}
		
		override protected function addEvt():void
		{
			down.addEventListener(MouseEvent.CLICK,onClick);
			up.addEventListener(MouseEvent.CLICK,onClick);
			
			bar.addEventListener(MouseEvent.MOUSE_DOWN,onBarMouseDown);
		}
		
		override protected function removeEvt():void
		{
			down.removeEventListener(MouseEvent.CLICK,onClick);
			up.removeEventListener(MouseEvent.CLICK,onClick);
			
			bar.removeEventListener(MouseEvent.MOUSE_DOWN,onBarMouseDown);
		}
		
		
		private function updataValue():void
		{
			_value = (bar.y - dragRect.y) / (dragRect.height - bar.height);
			if (Math.abs((bar.y + bar.height) - (dragRect.y + dragRect.height))<=1) _value = 1;
			executeCallBack(CHANGE);
		}
		
		///////////////////////////////////
		// evt handlers
		///////////////////////////////////
		
		private function onBarMouseDown(e:MouseEvent):void 
		{
			bar.stage.addEventListener(MouseEvent.MOUSE_UP, releaseBar);
			bar.stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraging);
			
			bar.startDrag(false,new Rectangle(bar.x,dragRect.y,0,dragRect.height-bar.height));
		}
		
		private function onDraging(e:MouseEvent):void 
		{
			updataValue();
		}
		
		private function releaseBar(e:MouseEvent):void 
		{
			bar.stage.removeEventListener(MouseEvent.MOUSE_UP, releaseBar);
			bar.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraging);
			bar.stopDrag();
		}
		
		private function onClick(e:MouseEvent):void 
		{
			if (e.currentTarget == up)
			{
				if (bar.y == dragRect.y) return;
				
				bar.y = Math.max(bar.y - interval, dragRect.y);
			}
			else
			{
				if (bar.y == dragRect.y + dragRect.height - bar.height) return;
				
				bar.y = Math.min(bar.y+interval,dragRect.y + dragRect.height - bar.height);
			}
			
			updataValue();
		}
		
		
		///////////////////////////////////
		// getters/setters
		///////////////////////////////////
		
		/**
		 * [0~1] 浮点数。滚动条 当前位置百分比
		 */
		public function get value():Number { return _value; }
		
		/** 滚动区域高度 **/
		public function get dragRectHeight():Number { return dragRect.height-bar.height;}
		
		/** 点击按钮bar 移动间隔 **/
		public function get interval():int { return _interval;}
		public function set interval(value:int):void 
		{
			_interval = value;
		}
		
	}

}